//
//  PersonalViewController.m
//  ZhuanZuBao
//
//  Created by 戴伟 on 2020/8/28.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "PersonalViewController.h"
#import "PostTypeViewController.h"
#import "SettingViewController.h"
#import "MyRentListViewController.h"
#import "MyCollectionViewController.h"
#import "EditInfoViewController.h"
#import "AboutViewController.h"
#import "ShareViewController.h"

@interface PersonalViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *avatarBtn;
@property (weak, nonatomic) IBOutlet UILabel *nickNameLabel;
@property (weak, nonatomic) IBOutlet UIView *levelBgView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation PersonalViewController

- (BOOL)fd_prefersNavigationBarHidden
{
    return YES;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if ([User isLogined]) {
        NSDictionary *dict = @{
            @"uid": [User sharedUser].userId,
            @"token": [User sharedUser].token
        };
        [NetWork sendGetUrl:@"user/get_user.php" withParams:dict success:^(id responseBody) {
            NSLog(@"%@", responseBody);
            User *user = [User sharedUser];
            user = [User mj_objectWithKeyValues:responseBody];
            
            self.nickNameLabel.text = [User sharedUser].nickname;
            [self.avatarBtn sd_setImageWithURL:[NSURL URLWithString:[User sharedUser].avatar] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"defaultAvatar"]];
        } failure:^(NSError *error) {
            
        }];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorColor = SEPARATECOLOR;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#f7f7f7" andAlpha:1];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#f7f7f7" andAlpha:1];
    
    self.nickNameLabel.text = [User sharedUser].nickname;
    _avatarBtn.layer.masksToBounds = YES;
    _avatarBtn.layer.cornerRadius = 25;
    
    [self.avatarBtn sd_setImageWithURL:[NSURL URLWithString:[User sharedUser].avatar] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"defaultAvatar"]];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 10;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, 10)];
        return view;
    }
    return nil;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.textLabel.textColor = BLACKTEXTCOLOR;
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.contentView.backgroundColor = [UIColor whiteColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"我的发布";
            cell.imageView.image = [UIImage imageNamed:@"personal2"];
        }
        if (indexPath.row == 1) {
            cell.textLabel.text = @"我的收藏";
            cell.imageView.image = [UIImage imageNamed:@"personal5"];
        }
        if (indexPath.row == 2) {
            cell.textLabel.text = @"成交记录";
            cell.imageView.image = [UIImage imageNamed:@"personal1"];
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"邀请有礼";
            cell.imageView.image = [UIImage imageNamed:@"personal8"];
        }
//        if (indexPath.row == 1) {
//            cell.textLabel.text = @"修改密码";
//            cell.imageView.image = [UIImage imageNamed:@"personal7"];
//        }
        if (indexPath.row == 1) {
            cell.textLabel.text = @"关于我们";
            cell.imageView.image = [UIImage imageNamed:@"personal3"];
        }
        if (indexPath.row == 2) {
            cell.textLabel.text = @"设置";
            cell.imageView.image = [UIImage imageNamed:@"personal4"];
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            MyRentListViewController *vc = [MyRentListViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row == 1) {
            MyCollectionViewController *vc = [MyCollectionViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row == 2) {
            
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            //分享
            ShareViewController *vc = [ShareViewController new];
            vc.link = @"http://share.cdlife.vip/zzb/index.html";
            vc.shareTitle = @"转租宝";
            vc.shareDesc = @"转租宝APP-最新版下载链接，欢迎下载，海量房源等你来看！";
            vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
            [self presentViewController:vc animated:NO completion:nil];
        }
        if (indexPath.row == 1) {
            AboutViewController *vc = [AboutViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row == 2) {
            SettingViewController *vc = [SettingViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

- (IBAction)post:(id)sender {
    PostTypeViewController *vc = [PostTypeViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)userInfo:(id)sender {
    EditInfoViewController *vc = [EditInfoViewController new];
    vc.editSuccessedBlock = ^{
        self.nickNameLabel.text = [User sharedUser].nickname;
    };
    [self.navigationController pushViewController:vc animated:YES];
}

@end
